//
//  YTLaunchImage.m
//  YTLaunchImage
//
//  Created by 张英堂 on 15/3/4.
//  Copyright (c) 2015年 zyt. All rights reserved.
//

#import "YTLaunchImage.h"
#import "AppDelegate.h"

@interface YTLaunchImage ()

@property (nonatomic, strong) UIViewController *rootViewController;
@property (nonatomic, strong) UIImageView *showImageView;

@end

@implementation YTLaunchImage

- (void)dealloc
{
    self.backgroundAction = nil;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.durationTime = 2;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (self.backgroundAction) {
        self.backgroundAction();
    }
    
    self.showImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.showImageView];
    [self.showImageView setImage:self.showImage];
}


- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    if (!self.rootViewController) {
        [[NSException exceptionWithName:@"设置错误" reason:@"请设置mainViewController" userInfo:nil] raise];
        return;
    }
    
    [self performSelector:@selector(pushMainViewController) withObject:nil afterDelay:self.durationTime];
}

- (void)pushMainViewController{
    
    UIImage *image = [self getImageFromView:self.view];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [self.rootViewController.view addSubview:imageView];
    
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    //修改rootViewController
    [delegate.window addSubview:self.rootViewController.view];
    delegate.window.rootViewController = self.rootViewController;
    
    [UIView animateWithDuration:0.3f
                     animations:^{
                         imageView.layer.transform = CATransform3DMakeScale(1.2, 1.2, 1.2);
                         imageView.alpha = 0.1;
                     }
                     completion:^(BOOL finished) {
                         [imageView removeFromSuperview];
                     }];
}

-(UIImage *)getImageFromView:(UIView *)view
{
    UIGraphicsBeginImageContext(view.bounds.size);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setMainViewController:(UIViewController *)mainViewController{
    self.rootViewController = mainViewController;
}






/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
